package com.hundsun.yr.universal.p2p.datahandler;

import android.content.Context;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.common.MD5Utils;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.yr.universal.p2p.bean.AdSlideBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.interactor.impl.StringRequestInteractorImpl;
import com.hundsun.yr.universal.p2p.listener.BaseMultiLoadedListener;
import com.hundsun.yr.universal.p2p.ui.activity.LoginActivity;
import com.hundsun.yr.universal.p2p.ui.activity.MineSettingActivity;
import com.hundsun.yr.universal.p2p.ui.activity.SetUpdatePhoneActivity;
import com.hundsun.yr.universal.p2p.utils.AndroidDes3Util;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.GsonTool;
import com.hundsun.yr.universal.p2p.utils.Logs;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

public class UpdatePhoneHandler extends BaseDataHandler {
    /**
     * 用户信息
     */
    private HSSharedPreferences sharedPreferences = null;
    private HSSharedPreferences mConfig = null;

    public UpdatePhoneHandler(Context mContext) {
        super(mContext);
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
        mConfig = new HSSharedPreferences(mContext,Config.SPPath.file_configInfo);
    }

    /**
     * 修改手机号
     */
    protected void doUpdatePhone(Object... obj) {
        final String newPhoneNum = obj[0].toString();
        String passwd = obj[1].toString();
        String vcode = obj[2].toString();

        StringRequestInteractorImpl requestinteractor = new StringRequestInteractorImpl(new BaseMultiLoadedListener<String>() {
            @Override
            public void onSuccess(String task_name, int event_tag, String data) {
                try {
                    AdSlideBeen adSlideBeen = (AdSlideBeen) GsonTool.getInstanceByJson(AdSlideBeen.class, data);
                    switch (adSlideBeen.code) {
                        case Constants.REQUESTCODE_000000:
                            String decode3 = null;
                            try {
                                decode3 = AndroidDes3Util.decode(adSlideBeen.data, mConfig.getShareProf(TaskConstants.TASK_CONFIG).substring(0, 24), (Date.parse(adSlideBeen.timestamp)+"").substring((Date.parse(adSlideBeen.timestamp)+"").length() - 8,(Date.parse(adSlideBeen.timestamp)+"").length()));
                                sharedPreferences.putShareProf(UserInfo.MOBILE, newPhoneNum);
                                BaseAppManager.getInstance().getActivity(SetUpdatePhoneActivity.class).finish();
                                BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
                                readyGo(LoginActivity.class);
                            } catch (Exception e) {
                                new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.date_error)).show();
                                e.printStackTrace();
                            }
                            break;
                        case Constants.REQUESTCODE_100500:
                            //验签失败
                            new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.login_overdue)).show();
                            break;
                        case Constants.REQUESTCODE_100020:
                            //Header值为空--不做任何提示
                            break;
                        default:
                            //请求异常情况
                            new HSToast(mContext).getSingleLongToast(adSlideBeen.msg).show();
                            break;
                    }
                }catch (Exception e){
                    new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.date_error)).show();
                    e.printStackTrace();
                }
                closeProgress();
            }

            @Override
            public void onException(String task_name, String msg) {
                closeProgress();
                showToast(msg);
            }
        });

        //修改手机号码网络请求
        String zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map = new JSONObject();

        try {
            map.put("phone", newPhoneNum);
            map.put("passwd", MD5Utils.getMD5String(passwd));
            map.put("vcode", vcode);
//            map.put("channel","0");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
        requestinteractor.getRequestData(TaskConstants.TASK_UPDATEPHONE, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_UPDATEPHONE, Constants.EVENT_DATA,
                jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_UPDATEPHONE, sharedPreferences
                        .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data));
    }

    /**
     * 发送验证码
     */
    protected void sendSms(Object... objs) {
        final String phone = objs[0].toString();

        StringRequestInteractorImpl requestinteractor = new StringRequestInteractorImpl(new BaseMultiLoadedListener<String>() {
            @Override
            public void onSuccess(String task_name, int event_tag, String data) {//请求成功
                try{
                    AdSlideBeen adSlideBeen = (AdSlideBeen) GsonTool.getInstanceByJson(AdSlideBeen.class, data);
                    switch (adSlideBeen.code) {
                        case Constants.REQUESTCODE_000000:
                            String decode3 = null;
                            try {
                                decode3 = AndroidDes3Util.decode(adSlideBeen.data, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), (Date.parse(adSlideBeen.timestamp) + "").substring((Date.parse(adSlideBeen.timestamp) + "").length() - 8, (Date.parse(adSlideBeen.timestamp) + "").length()));
                                new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.sms_success)).show();
                            } catch (Exception e) {
                                new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.date_error)).show();
                                e.printStackTrace();
                            }
                            break;
                        case Constants.REQUESTCODE_100500:
                            //验签失败
                            new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.login_overdue)).show();
                            break;
                        case Constants.REQUESTCODE_100020:
                            //Header值为空--不做任何提示
                            break;
                        default:
                            //请求异常情况
                            new HSToast(mContext).getSingleLongToast(adSlideBeen.msg).show();
                            break;
                    }
                }catch (Exception e){
                    new HSToast(mContext).getSingleLongToast(mContext.getString(R.string.date_error)).show();
                   e.printStackTrace();
                }
                closeProgress();
            }

            @Override
            public void onException(String task_name, String msg) {
                Logs.e(TAG, "error:" + msg);
                closeProgress();
                showToast(msg);
            }
        });

        String zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map = new JSONObject();

        try {
            map.put("type", "20");
            map.put("phone", phone);
            map.put("channel","0");
//            map.put("channel","0");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
        requestinteractor.getRequestData(TaskConstants.TASK_SENDSMS, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SENDSMS, Constants.EVENT_DATA,
                jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SENDSMS, sharedPreferences
                        .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data));
    }
}
